<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
用户积分控制器
*/
class Credit extends MY_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->__validation();
        $this->load->model('Account_model','Account');
    }

    private function __checkinput()
    {
        if(!is_numeric($this->params['uid']))
        {
            $this->setResponse('USER_ID_IS_EMPTY');
            $this->_print();
            exit;
        }
       
    }
    /*查询积分*/
    public function query()
    {
        $this->__checkinput();
        $rtn = $this->Account->queryUserCredit($this->params['uid']);
        if(!$rtn)
        {
            $this->setResponse('USER_IS_NOT_FOUND');
        }
        else
        {
            $this->response['code'] = 0;
            $this->response['message']='OK';
            $this->response['data'] = array('credit'=>(int)$rtn->credit);
        }
        $this->_print();
    }

    /*增加积分*/
    public function incr()
    {
        $this->__checkinput();
        $amount = intval($this->params['amount']);
        $this->Account->incrCredit($this->params['uid'],$amount);
        $this->setResponse('OK');
        $this->_print();
    }


    /*扣除积分*/
    public function decr()
    {
        $this->__checkinput();
        $amount = intval($this->params['amount']);
        $rtn = $this->Account->decrCredit($this->params['uid'],$amount);
        if($rtn)
        {
            $this->setResponse('OK');
        }
        else
            $this->setResponse('AMOUNT_IS_LESS');
        $this->_print();
    }
    public function index()
    {
        exit;
    }
}